home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / FREENET / BRODIE / OPENSOCK.C < prev    next >
C/C++ Source or Header  |  1996-02-25  |  4KB  |  147 lines

  1. /* opensocks.c / openfiles.c */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <errno.h>
  8. #include "netinet/in.h"
  9. #include "sys/socket.h"
  10. #include "sys/errno.h"
  11. #include "sys/byteorder.h"
  12. #include "arpa/inet.h"
  13. #include "netdb.h"
  14. #include "bind:dnsquery.h"
  15. #include "oslib:osargs.h"
  16.  
  17. static void strlwr(char *c)
  18. {
  19.         while (*c) {
  20.                 *c = tolower(*c); ++c;
  21.         }
  22. }
  23.  
  24. static void port(char *buffer, int port2, int sock)
  25. {
  26.     struct servent *s;
  27.         char *proto;
  28.         char buf[16];
  29.         int len = 16;
  30.         int res;
  31.  
  32.         res  = getsockopt(sock, SOL_SOCKET, SO_TYPE, buf, &len);
  33.         if (res != -1) switch (*buf) {
  34.                 case SOCK_STREAM: proto="tcp"; break;
  35.                 case SOCK_DGRAM: proto="udp"; break;
  36.                 case SOCK_RAW: proto="icmp"; break;
  37.                 default: proto=""; break;
  38.         }
  39.         else proto = "";
  40.  
  41.     s = getservbyport(port2, (*proto)?proto:NULL);
  42.  
  43.     if (s) {
  44.             strcat(buffer, s->s_name);
  45.         strcat(buffer, "/");
  46.                 strcat(buffer, proto);
  47.     }
  48.     else {
  49.             sprintf(buf, "%d/%s", ntohs(port2),proto);
  50.             strcat(buffer, buf);
  51.     }
  52.  
  53. }
  54.  
  55. static char *ip(int s, struct sockaddr_in *sin)
  56. {
  57.         static char    buffer[256];
  58.         struct hostent *host;
  59.  
  60.         strcpy(buffer, inet_ntoa(sin->sin_addr));
  61.         if (sin->sin_addr.s_addr == INADDR_ANY) {
  62.                 strcpy(buffer, "[INADDR_ANY]");
  63.         } else if ((sin->sin_addr.s_addr & 0xff) == 0x7f) {
  64.                 strcpy(buffer, "[localhost]");
  65.         } else {
  66.                 dnsquery *q = dnsquery_gethostbyaddr(
  67.                     (char *)&sin->sin_addr, 4, AF_INET);
  68.                 if (q) {
  69.                         dns_status s;
  70.                 do {
  71.                         s = dnsquery_check(q);
  72.                 } while (s != dns_query_complete_success &&
  73.                  s != dns_query_complete_failure);
  74.                 if (s == dns_query_complete_success) {
  75.                 host = dnsquery_getanswer(q);
  76.                 if (host) {
  77.                         buffer[0] = '\0';
  78.                         strncat(buffer, host->h_name, 200);
  79.                 }
  80.                 }
  81.                 dnsquery_dispose(q);
  82.                 }
  83.         }
  84.  
  85.     strcat(buffer, " (");
  86.     port(buffer, sin->sin_port, s);
  87.     return strcat(buffer, ")");
  88. }
  89.  
  90. static int opensocks(char *flags)
  91. {
  92.         struct sockaddr_in    sin;
  93.         int            size;
  94.         int            s;
  95.     extern int gettsize(void);
  96.     int            maxs = gettsize();
  97.     const int noexist = !!strchr(flags, 'a');
  98.     const int noerror = !!strchr(flags, 'e');
  99.  
  100.         for (s=0; s<maxs; ++s) {
  101.                 size = 16;
  102.                 errno = 0;
  103.                 if (getsockname(s, (struct sockaddr *)&sin, &size) < 0) {
  104.                         if (((errno==EBADF) && noexist) ||
  105.                              (errno!=EBADF) && noerror) {
  106.                                 printf("%8d (errno %d)\n", s, errno);
  107.                         }
  108.                         continue;
  109.                 }
  110.  
  111.                 printf("%8d %s", s, ip(s,&sin));
  112.                 size = 16;
  113.                 if (getpeername(s, (struct sockaddr *)&sin, &size) > -1) {
  114.                         printf(" <---> %s", ip(s,&sin));
  115.                 }
  116.                 putchar('\n');
  117.         }
  118.  
  119.         return 0;
  120. }
  121.  
  122. static int openfiles(void)
  123. {
  124.         int     t,ptr,ext;
  125.         bits    stream_status, fs_status;
  126.         char    name[256];
  127.  
  128.     printf("Handle Extent CPointer Name\n");
  129.         for (t=255; t>0; --t) {
  130.                 osargs_read_info((os_f) t, &stream_status, &fs_status);
  131.                 if (stream_status & osargs_STREAM_UNALLOCATED) continue;
  132.         ext = osargs_read_ext((os_f) t);
  133.         ptr = osargs_read_ptr((os_f) t);
  134.                 osargs_read_path((os_f) t, name, 256);
  135.                 printf("%4d %8X %8X %s\n", t, ext, ptr, name);
  136.         }
  137.         return 0;
  138. }
  139.  
  140. int main(int argc, char *argv[])
  141. {
  142.         argc=argc;
  143.         strlwr(argv[0]);
  144.         if (strstr(argv[0],"opensocks")) return opensocks(argv[1]);
  145.         else return openfiles();
  146. }
  147.